<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    #di{
      width: 100px;
      height: 100px;
      background-color: yellow;
      position: absolute;
      top: 100px;
      left: 0;
    }
  </style>
</head>
<body>
  <input type="button" value="按钮" id="btn">
  <input type="button" value="停止" id="btn1">
  <div id="di"></div>
  <script>
    var btn = document.getElementById("btn");
    var btn1 = document.getElementById("btn1");

    var di = document.getElementById("di");
    // 点击按钮盒子移动
     var set = 0;
      // 信号量，初始值与源属性保持一致
      var dstep = 0;
    btn.onclick = function () {

      // 定时器制作运动过程
      set = window.setInterval(function () {
        dstep += 5;
        // 将新值赋值给盒子
        di.style.left = dstep + "px";
      },100)
    }
    // 结束运动(开始和结束写在不同函数内，容易出现多次误点击事件---导致移动加速，并且停止失效)
    btn1.onclick = function () {
      console.log(dstep);
      clearInterval(set);
    }

  </script>
</body>
</html>